//
//  CMSChartsViewController.m
//  CMSCharts
//
//  Created by symbio on 2023/2/14.
//

#import "CMSChartsViewController.h"
#import "CMSChartsView.h"
#import <Masonry/Masonry.h>
#import <CMSUIComponents/CMSTheme.h>

@interface CMSChartsViewController ()
@property (nonatomic, strong) CMSChartsView *chartsView;

/// 行情数据
@property (nonatomic, strong) NSArray<NSDictionary *> *arrChartData;

@end

@implementation CMSChartsViewController

- (CMSChartsView *)chartsView {
    if (!_chartsView) {
        _chartsView = [[CMSChartsView alloc] init];
    }
    return _chartsView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Do any additional setup after loading the view.
    [self setupUI];
    
    [self reloadData];
    
    self.view.cms_themeState = CMSThemeStateSupport;
    
}

- (void)setupUI {
    [self.view addSubview:self.chartsView];
    [self.chartsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(550);
    }];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"切换皮肤" style:(UIBarButtonItemStylePlain) target:self action:@selector(changeTheme)];
}

- (void)reloadData {
    self.arrChartData = [self getJsonWithName:@"USD_min5"];
    self.chartsView.chartConfigs = [self getJsonWithName:@"chartConfig"];
    
    self.chartsView.arrChartData = self.arrChartData;
    [self.chartsView reloadData];
}

- (id)getJsonWithName:(NSString *)name {
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    return [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:NULL];
}

- (void)changeTheme {
    static int i = 0;
    i++;
    [[CMSThemeManager sharedInstance] changeThemeWithName: i % 2 == 0 ? @"light" : @"dark"];
    
    [_chartsView reloadData];
}

@end
